﻿using AJWPFAdmin.Core.Utils;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace AJWPFAdmin.Core.Components.AJCarousel.ViewModels
{
    /// <summary>
    /// 轮播组件配置项
    /// </summary>
    public class AJCarouselOptions : BindableBase
    {

        private double _height = 320;
        /// <summary>
        /// 高度， 默认 320
        /// </summary>
        public double Height
        {
            get { return _height; }
            set { SetProperty(ref _height, value); }
        }

        private double _width = 480;
        /// <summary>
        /// 宽度， 默认 480
        /// </summary>
        public double Width
        {
            get { return _width; }
            set { SetProperty(ref _width, value); }
        }

        private bool _autoPlay = true;
        /// <summary>
        /// 是否自动播放， 默认 true
        /// </summary>
        public bool AutoPlay
        {
            get { return _autoPlay; }
            set { SetProperty(ref _autoPlay, value); }
        }

        private double _speed = 5000;
        /// <summary>
        /// 自动播放速度， 5000 毫秒，（5s）
        /// </summary>
        public double Speed
        {
            get { return _speed; }
            set
            {
                SetProperty(ref _speed, value);
            }
        }



        private ObservableCollection<AJCarouselImage> _iamges;
        /// <summary>
        /// 轮播图列表
        /// </summary>
        public ObservableCollection<AJCarouselImage> Images
        {
            get { return _iamges; }
            set { SetProperty(ref _iamges, value); }
        }
    }

    public class AJCarouselImage : BindableBase
    {
        private BackgroundWorker _imgDownloadWorker;

        private string _filePath;
        public string FilePath
        {
            get { return _filePath; }
            set
            {
                if (SetProperty(ref _filePath, value))
                {
                    if (_imgDownloadWorker.IsBusy)
                    {
                        return;
                    }
                    _imgDownloadWorker.RunWorkerAsync(_filePath);
                }
            }
        }

        private Stretch _stretch = Stretch.Uniform;
        /// <summary>
        /// 图片Stretch， 默认 Uniform
        /// </summary>
        public Stretch Stretch
        {
            get { return _stretch; }
            set
            {
                SetProperty(ref _stretch, value);
            }
        }

        public string Name { get; set; }
        public string Info { get; set; }

        private BitmapImage _image;
        /// <summary>
        /// 不建议直接复制， 只需要赋值 FilePath 即可
        /// </summary>
        public BitmapImage Image
        {
            get { return _image; }
            set { SetProperty(ref _image, value); }
        }

        public AJCarouselImage()
        {
            _imgDownloadWorker = new BackgroundWorker();
            _imgDownloadWorker.DoWork += (_, e) =>
            {
                var stream = CommonUtil.GetImageStreamFromLocalOrHttp(e.Argument as string, out var __);
                if (stream != null)
                {
                    stream = CommonUtil.GetImageStreamFromLocalOrHttp(Path.Combine(AppContext.BaseDirectory, "Images\\media-empty.png"), out __);
                }
                e.Result = stream;
            };
            _imgDownloadWorker.RunWorkerCompleted += (_, e) =>
            {
                if (e.Error != null || e.Result == null)
                {
                    return;
                }
                Image = CommonUtil.CreateBitmapImageFromStream(e.Result as Stream);
            };
        }
    }
}
